package org.jeecg.modules.my.controller;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.test.entity.JeecgDemo;
import org.jeecg.modules.demo.test.service.IJeecgDemoService;
import org.jeecg.modules.my.utils.HttpClientSslUtils;
import org.jeecg.modules.my.utils.JsonUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Api(tags = "测试小程序登录")
@RestController
@RequestMapping("/wxlogin")
@Slf4j
public class WXLoginController extends JeecgController<JeecgDemo, IJeecgDemoService> {

    /**
     * 微信小程序手机号码快捷登录
     *
     export const postLoginWxMinAPI = (data: LoginParams) => {
         return http<LoginResult>({
         method: 'POST',
         url: 'http://localhost:8080/jeecg-boot/wxlogin/wxMin',
         data,
         })
     }
     //获取code
     let code1 = '';
     onLoad(async () => {
         const res = await wx.login();
         code1 = res.code
         console.log("code>>>>", res)
     })

     const onGetphonenumber: UniHelper.ButtonOnGetphonenumber = async (ev) => {
         console.log(">>>>>>>", ev)

         const { code, encryptedData, iv } = ev.detail
         const res = await postLoginWxMinAPI({ code, encryptedData, iv })
         console.log(res)
     }

     //根据wx.login进入时获取的code获取用户唯一标识openid
     https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
     * @param wxDate
     * @return
     */
    @PostMapping("/wxMin")
    public Result<Object> getPhone(@RequestBody Map<String,String> wxDate) {
        JSONObject wxJson;

        // 获取token
        String token_url = String.format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
                "wx5c172d18bc021d4f", "1ecaab8de2218a808018b264736fba49");

        try {
            JSONObject token = JSON.parseObject(HttpClientSslUtils.doGet(token_url));
            if (token == null) {
                log.info("获取token失败");
                return null;
            }
            String accessToken = token.getString("access_token");
            if (StringUtils.isEmpty(accessToken)) {
                log.info("获取token失败");
            }
            //获取phone
            String url = "https://api.weixin.qq.com/wxa/business/getuserphonenumber"
                    + "?access_token=" + accessToken;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code", wxDate.get("code"));
            String reqJsonStr = JsonUtil.objToString(jsonObject);
            wxJson = JSON.parseObject(HttpClientSslUtils.doPost(url, reqJsonStr));

            if (wxJson == null) {
                log.info("获取手机号失败");
                return Result.error("获取手机号失败!");
            }
            return Result.OK("获取成功!", wxJson);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.error("获取失败,请重试！");
    }
}
